//
//  OSPublishViewController.m
//  百思不得姐
//
//  Created by PushMeBaby on 16/3/7.
//  Copyright © 2016年 CoderC. All rights reserved.
//

#import "OSPublishViewController.h"
#import "OSVerticalButton.h"
#import <POP.h>
#import "OSNavgationViewController.h"
#import "OSPostWordViewController.h"
static CGFloat const OSAnimationDelay = 0.1;
static CGFloat const OSSpringFactor = 10;
@interface OSPublishViewController ()

@end

@implementation OSPublishViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.view.userInteractionEnabled = NO;
    
    NSArray *images = @[@"publish-video", @"publish-picture", @"publish-text", @"publish-audio", @"publish-review", @"publish-offline"];
    NSArray *titles = @[@"发视频", @"发图片", @"发段子", @"发声音", @"审帖", @"离线下载"];
    
    int maxCols = 3;
    CGFloat buttonW = 72;
    CGFloat buttonH = buttonW + 30;
    CGFloat buttonStartY = (OSScreenH - 2 * buttonH) * 0.5;
    CGFloat buttonStartX = 20;
    CGFloat xMargin = (OSScreenW - 2 * buttonStartX - maxCols * buttonW) / (maxCols - 1);
    
    for (int i = 0; i < images.count; i++) {
        OSVerticalButton *button = [[OSVerticalButton alloc]init];
        button.tag =  i;
        
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
        // 设置内容
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        [button setTitle:titles[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:images[i]] forState:UIControlStateNormal];
        
        
        int row = i / maxCols;
        int col = i % maxCols;
        CGFloat buttonX = buttonStartX + col * (xMargin + buttonW);
        CGFloat buttonEndY = buttonStartY + row * buttonH;
        CGFloat buttonBeginY = buttonEndY - OSScreenH;
        
        
        POPSpringAnimation *anim = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame];
        anim.fromValue = [NSValue valueWithCGRect:CGRectMake(buttonX, buttonBeginY, buttonW, buttonH)];
        anim.toValue = [NSValue valueWithCGRect:CGRectMake(buttonX, buttonEndY, buttonW, buttonH)];
        anim.springBounciness = OSSpringFactor;
        anim.springSpeed = OSSpringFactor;
        anim.beginTime = CACurrentMediaTime() + OSAnimationDelay * i;
        [button pop_addAnimation:anim forKey:nil];
    }
    
    UIImageView *sloganView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"app_slogan"]];
    [self.view addSubview:sloganView];
    
    POPSpringAnimation *anim = [POPSpringAnimation animationWithPropertyNamed:kPOPViewCenter];
    CGFloat centerX = OSScreenW * 0.5;
    CGFloat centerEndY = OSScreenH * 0.2;
    CGFloat centerbBeginY = centerEndY - OSScreenH;
    anim.fromValue = [NSValue valueWithCGPoint:CGPointMake(centerX, centerbBeginY)];
    anim.toValue = [NSValue valueWithCGPoint:CGPointMake(centerX, centerEndY)];
    anim.beginTime = CACurrentMediaTime() + images.count * OSAnimationDelay;
    anim.springBounciness = OSSpringFactor;
    anim.springSpeed = OSSpringFactor;
    [anim setCompletionBlock:^(POPAnimation * anim, BOOL finished) {
        
        self.view.userInteractionEnabled = YES;
        
    }];
    [sloganView pop_addAnimation:anim forKey:nil];
    
    
    
    
}

-(void)cancelWithCompletionBlock:(void(^)())completionBlock{
    
    self.view.userInteractionEnabled = NO;
    
    int beginIndex = 2;
    for (int i = beginIndex; i < self.view.subviews.count; i++) {
        
        
        UIView *subView = self.view.subviews[i];
        
        POPBasicAnimation *anim = [POPBasicAnimation animationWithPropertyNamed:kPOPViewCenter];
        CGFloat centerY = subView.centerY + OSScreenH;
        anim.toValue = [NSValue valueWithCGPoint:CGPointMake(subView.centerX, centerY)];
        anim.beginTime = CACurrentMediaTime() + (i - beginIndex) * OSAnimationDelay;
        [subView pop_addAnimation:anim forKey:nil];
        
        if (i == self.view.subviews.count - 1) {
            [anim setCompletionBlock:^(POPAnimation *anim, BOOL finished) {
                [self dismissViewControllerAnimated:YES completion:nil];
                
                !completionBlock ? : completionBlock();
                
                
            }];
        }
    }
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self cancelWithCompletionBlock:nil];
}
-(IBAction)buttonClick:(UIButton*)button{
    [self cancelWithCompletionBlock:^{
        if (button.tag == 0) {
            NSLog(@"发视频");
        }
        if (button.tag == 2) {
            OSPostWordViewController *postWordVc  = [[OSPostWordViewController alloc]init];
            OSNavgationViewController *nav = [[OSNavgationViewController alloc]init];
            [nav addChildViewController:postWordVc];
            
            UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController;
            
            [vc presentViewController:nav animated:YES completion:nil];
            
        }
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
